package com.zf.dtp.web;

import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletRequest;
import net.zf.dynamic.threadpool.bean.ThreadPoolConfigEntity;
import net.zf.dynamic.threadpool.util.ThreadPoolRegistryEnum;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;

@Controller
@RequestMapping("/monitor")
public class ThreadPoolMonitorController {

    private static final Logger log = LoggerFactory.getLogger(ThreadPoolMonitorController.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient dynamicRedissonClient;

    @RequestMapping("/toIndex")
    public String index(HttpServletRequest request, Model model) {
        String key =  ThreadPoolRegistryEnum.THREAD_POOL_CONFIG_LIST_KEY.getCode();
        RList<ThreadPoolConfigEntity> threadPoolConfigEntityRList = dynamicRedissonClient.getList(ThreadPoolRegistryEnum.THREAD_POOL_CONFIG_LIST_KEY.getCode());
        model.addAttribute("threadPoolConfigEntityList", threadPoolConfigEntityRList);
        return "index";
    }

    @ResponseBody
    @GetMapping("/thread/pool/list")
    public String threadPoolList() {
        String key =  ThreadPoolRegistryEnum.THREAD_POOL_CONFIG_LIST_KEY.getCode();
        RList<ThreadPoolConfigEntity> threadPoolConfigEntityRList = dynamicRedissonClient.getList(ThreadPoolRegistryEnum.THREAD_POOL_CONFIG_LIST_KEY.getCode());
        return JSONUtil.toJsonStr(threadPoolConfigEntityRList);
    }

    @PostMapping("/thread/pool/config")
    @ResponseBody
    public String threadPoolConfig(@RequestBody ThreadPoolConfigEntity threadPoolConfigEntity) {
        int coreSize = threadPoolConfigEntity.getCorePoolSize();
        int maxPoolSize = threadPoolConfigEntity.getMaxPoolSize();
        log.info("修改线程池参数：coreSize:{},maxPoolSize:{}", coreSize, maxPoolSize);

        return "success";
    }

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor_1;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor_2;

    @GetMapping("/mock")
    @ResponseBody
    public String mockThreadRun(){
        for(int i=0;i<100;i++){
            threadPoolExecutor_1.submit(() -> {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("thread_pool_1中线程：{} 正在运行", Thread.currentThread().getName());
            });
        }

        for(int i=0;i<200;i++){
            threadPoolExecutor_2.submit(() -> {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("thread_pool_2中线程：{} 正在运行", Thread.currentThread().getName());
            });
        }

        return "success";
    }


}
